//index.js
//获取应用实例
const app = getApp();

const api = require("../../config.js");

Page({
	data: {
		userInfo: {},
		is_loading:true
	},
	
	onLoad: function () {
		this.login();
	},


	login() {
		wx.getSetting({
			success: res => {
				if (res.authSetting['scope.userInfo']) {
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
					wx.login({
						success: (r) => {
							wx.getUserInfo({
								withCredentials: true,
								success: res => {
									// 可以将 res 发送给后台解码出 unionId
									res.code = r.code;
									wx.request({
										data: res,
										method: "POST",
										url: api.api+"api/wechat_login",
										success: (res) => {
											this.setData({ userInfo: res.data, is_loading:false});
											wx.setStorageSync('userInfo', JSON.stringify(this.data.userInfo));
										}
									});

									if (this.userInfoReadyCallback) {
										this.userInfoReadyCallback(res)
									}
								}
							});
						}
					});
				} else {
					console.log("尚未授权");
					this.dialog = this.selectComponent("#J_Popup");
					this.dialog && this.dialog.show();
				}
			}
		});
	},

	onGotUserInfo(e){
		this.dialog = this.selectComponent("#J_Popup");
		this.dialog && this.dialog.hide();
		this.login();
	},

	//快速开始游戏
	fast_game(){
		wx.navigateTo({
			url: '../canvas/canvas?type=fast'
		})
	},
	create_room(){
		console.log("123");
	}
});
